package com.orangelife.mobile.complaints.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;
import com.orangelife.mobile.complaints.activity.ComplainDetailActivity;
import com.orangelife.mobile.util.DateUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by YangWei
 * on 2015/11/12.
 */
public class MyComplainListAdapter<T> extends FatherAdapter<T> {
    private Context context;
    private List<Map<String, Object>> list;

    public MyComplainListAdapter(Context context,
                                 List<Map<String, Object>> list) {

        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public BaseAdapter setList(List<T> list) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_lv_my_complain, null);
            holder.llComplain = (LinearLayout)convertView.findViewById(R.id.llComplain);
            holder.tvComplainTime = (TextView)convertView.findViewById(R.id.tvComplainTime);
            holder.tvComplainStatus = (TextView)convertView.findViewById(R.id.tvComplainStatus);
            holder.tvComplainContent = (TextView)convertView.findViewById(R.id.tvComplainContent);
            holder.ivRight = (ImageView)convertView.findViewById(R.id.ivRight);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Map<String,Object> map=list.get(position);
        DateUtil dateUtil = new DateUtil();
        String time = dateUtil.tranStringForDate(String.valueOf(list
                .get(position).get("tscreate")));
        holder.tvComplainTime.setText(time);
        holder.tvComplainContent.setText(String.valueOf(list.get(position).get("content")));
        int status = Integer.parseInt(String.valueOf(list.get(position).get("status")));
        String str = "";
        switch (status) {
            case 0:
                str = "未处理";
                break;
            case 1:
                str = "已受理";
                break;
            case 2:
                str = "处理中";
                break;
            case 3:
                str = "已完成";
                break;
            case 4:
                str = "已评价";
                break;
        }
        holder.tvComplainStatus.setText(str);
        holder.llComplain.setOnClickListener(new listener(map));

        return convertView;
    }

    class listener implements View.OnClickListener {
        private Map<String, Object> map = null;

        public listener(Map<String, Object> map) {

            this.map = map;
        }

        Intent intent = new Intent();

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.llComplain:
                    intent.putExtra("complaintID", String.valueOf(map.get("complaintID")));
                    intent.setClass(context,
                            ComplainDetailActivity.class);
                    context.startActivity(intent);
                    break;
                case R.id.ivRight:
                    intent.putExtra("complaintID", String.valueOf(map.get("complaintID")));
                    intent.setClass(context,
                            ComplainDetailActivity.class);
                    context.startActivity(intent);
                    break;

                default:
                    break;
            }
        }
    };

    class ViewHolder {
        LinearLayout llComplain;
        TextView tvComplainTime;
        TextView tvComplainStatus;
        TextView tvComplainContent;
        ImageView ivRight;
    }
}
